﻿using Seggu.Dtos;
using Seggu.Services;
using Seggu.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Seggu.Desktop.Forms
{ 
    public partial class Productores : Form
    {
        private IProducerService producerService;
        private ProducerDto currentProducer = new ProducerDto();
        private bool isNew;
        public Productores(IProducerService producerService)
        {
            this.producerService = producerService;
            InitializeComponent();
            InitializeIndex();
        }

        private void InitializeIndex()
        {
            isNew = false;
            var table = GetProducerDataTable();
            //grdProductores.DataSource = table;
            var producers = producerService.GetProducers().ToList();
            grdProductores.DataSource = producers;
            grdProductores.Columns["Id"].Visible = false;
            grdProductores.Columns["Cobrador"].Visible = false;
            grdProductores.Focus();
            ClearAndAddDataBindings(producers);
        }
            private DataTable GetProducerDataTable()
            {
                var table = new DataTable();
                table.Columns.Add("Id", typeof(string));
                table.Columns.Add("Nombre", typeof(string));
                table.Columns.Add("Matrícula", typeof(string));
                table.Columns.Add("Cobrador");

                var list = this.producerService.GetProducers();

                foreach (var x in list)
                {
                    var row = table.NewRow();
                    row["Id"] = x.Id;
                    row["Nombre"] = x.Name;
                    row["Matrícula"] = x.Matrícula;
                    row["Cobrador"] = x.Cobrador;
                    table.Rows.Add(row);
                }
                return table;
            }
            private void ClearAndAddDataBindings(List<ProducerDto> prod)
            {
                txtNombre.DataBindings.Clear();
                txtMatricula.DataBindings.Clear();
                chkCobrador.DataBindings.Clear();

                txtNombre.DataBindings.Add("text", prod, "Name");
                txtMatricula.DataBindings.Add("text", prod, "Matrícula");
                chkCobrador.DataBindings.Add("checked", prod, "Cobrador");
            }


        private void btnEditar_Click(object sender, EventArgs e)
        {
            if (!txtMatricula.Visible)
            {
                txtMatricula.Visible = true;
                txtNombre.Visible = true;
                chkCobrador.Visible = true;
                btnGuardar.Visible = true;
                btnQuitar.Visible = true;
                btnNuevo.Visible = true;
            }
            else
            {
                txtMatricula.Visible = false;
                txtNombre.Visible = false;
                chkCobrador.Visible = false;
                btnGuardar.Visible = false;
                btnQuitar.Visible = false;
                btnNuevo.Visible = false;
            }
        }

        private void btnQuitar_Click(object sender, EventArgs e)
        {
            producerService.Delete(grdProductores.SelectedCells[3].Value.ToString());
            InitializeIndex();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var producer = this.GetProducerFromForm();
            producerService.Save(producer);
            this.InitializeIndex();
        }
            private ProducerDto GetProducerFromForm()
            {
                currentProducer.Id = isNew ? null : currentProducer.Id;
                currentProducer.Name = txtNombre.Text;
                currentProducer.Matrícula = txtMatricula.Text;
                currentProducer.Cobrador = chkCobrador.Checked ? true : false;
                return currentProducer;
            }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            isNew = true;
            grdProductores.ClearSelection();
            txtNombre.Clear();
            txtMatricula.Clear();
            chkCobrador.Checked = false;
        }

        private void grdProductores_SelectionChanged(object sender, EventArgs e)
        {
            currentProducer = (ProducerDto)grdProductores.CurrentRow.DataBoundItem;
        }
    }
}
